package com.bawei.day15_service2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

/***
 1。前台服务：服务默认是后台，当系统内存不足时会被回收，若不想被回收，写成前台服务：通知
 2。avtivity中设置点击：调用服务提供的方法---->绑定服务
 3。通知上设置点击：调用服务提供的方法----》广播接受者
 */
public class MainActivity extends AppCompatActivity {
    private MusicService.MusicBinder musicBinder;
    private MusicRecevier musicRecevier;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicBinder = (MusicService.MusicBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //启动服务
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        bindService(intent,connection, Service.BIND_AUTO_CREATE);
        //注册广播
        musicRecevier = new MusicRecevier();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.bawei.pre");
        intentFilter.addAction("com.bawei.play");
        intentFilter.addAction("com.bawei.next");
        registerReceiver(musicRecevier,intentFilter);


        findViewById(R.id.bt_pre).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicBinder.pre();
            }
        });
        findViewById(R.id.bt_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicBinder.next();
            }
        });
        findViewById(R.id.bt_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicBinder.play();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(musicRecevier);
    }

    //    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        Intent intent = new Intent(this, MusicService.class);
//        unbindService(connection);
//        stopService(intent);
//    }

    class MusicRecevier extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if("com.bawei.pre".equals(intent.getAction())){
                musicBinder.pre();
            }else if("com.bawei.play".equals(intent.getAction())){
                musicBinder.play();
            }else if("com.bawei.next".equals(intent.getAction())){
                musicBinder.next();
            }

        }
    }
}
